//
//  BQHomeViewController.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/4/26.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQHomeViewController.h"
#import "BQHomeLayout.h"
#import "BQCycleCell.h"
#import "BQTypeCVCell.h"
#import "BQNewsCell.h"
#import "BQShopCell.h"
#import "BQHomeModel.h"
#import "BQAccountManager.h"
#import "BQRightModel.h"
#import "BQShopCartViewController.h"
#import "UIColor+HMAddition.h"
#import "BQOrderViewController.h"
@interface BQHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CAAnimationDelegate>

@property (nonatomic,weak) UICollectionView *homeCV;


@end
static NSString *Cycel_Cell = @"Cycel_Cell";
static NSString *Type_Cell  = @"Type_Cell";
static NSString *News_Cell  = @"News_Cell";
static NSString *Shop_Cell  = @"Shop_Cell";
@implementation BQHomeViewController{
    
    /// 数据源数组
    NSArray *_dataArray;
    
    NSArray *_shopArray;
    
    CALayer *_layer;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    
    self.navigationController.navigationBar.barTintColor =[UIColor hm_colorWithR:252 G:207 B:47];
    
}

- (void)setupUI{
    
    [super setupUI];
    
    
//
    
    //    [self registerNotification];
    
    UICollectionView *homeCV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:[BQHomeLayout new]];
    
    homeCV.backgroundColor = [UIColor hm_colorWithR:242 G:242 B:242];
    
    _homeCV = homeCV;
    
    homeCV.delegate = self;
    
    homeCV.dataSource = self;
    
    [homeCV registerClass:[BQCycleCell class] forCellWithReuseIdentifier:Cycel_Cell];
    
    [homeCV registerClass:[BQTypeCVCell class] forCellWithReuseIdentifier:Type_Cell];
    
    [homeCV registerClass:[BQNewsCell class] forCellWithReuseIdentifier:News_Cell];
    
    [homeCV registerNib:[UINib nibWithNibName:@"BQShopCell" bundle:nil] forCellWithReuseIdentifier:Shop_Cell];
    
    [self.view addSubview:homeCV];
    
    [homeCV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(-64);
        
        make.left.bottom.right.offset(0);
    }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha =(scrollView.contentOffset.y + 64)/(180 - 64);
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 4;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 2) {
        return _dataArray.count;
    }
    
    if(section == 3){
        
        return _shopArray.count;
        
    }
    
    return 1;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section == 0) {
        BQCycleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cycel_Cell forIndexPath:indexPath];
        return cell;
    }else if(indexPath.section == 1){
        BQTypeCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Type_Cell forIndexPath:indexPath];
        
        return cell;
    }else if(indexPath.section == 2){
        
        BQNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:News_Cell forIndexPath:indexPath];
        
        BQHomeModel *model = _dataArray[indexPath.item];
        
        cell.model = model;
        
        return cell;
        
    }else{
        
        BQShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Shop_Cell forIndexPath:indexPath];
        
        BQRightModel *model = _shopArray[indexPath.item];
        
        cell.rightModel = model;
        
        return cell;
        
    }
}


//修改大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.section == 0 )
    {
        return CGSizeMake(_ScreenWidth,180);
    }else if(indexPath.section == 1){
        return CGSizeMake(_ScreenWidth, 100);
    }else if(indexPath.section == 2){
        return CGSizeMake(_ScreenWidth - 20, 200);
    }else{
        return CGSizeMake((_ScreenWidth - 10 * 3)/2, 230);
    }
}

//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section == 2 || section == 3) {
        return 10;
    }
    
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    if (section == 2 || section == 3) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


-(void)loadData{
    
    NSDictionary *para = @{@"call" : @1};
    
    [_AccountMgr POSTWithUrlString:focus_url parameters:para success:^(id responseObject) {
        
        // 获取分类模型数组
        NSArray *dictArray = responseObject[@"data"][@"activities"];
        
        _dataArray = [NSArray yy_modelArrayWithClass:[BQHomeModel class] json:dictArray];
        
        [self.homeCV reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    FMDatabase *db = [[FMDatabase alloc] initWithPath:localDBPath];
    [db open];
    
    NSDictionary *para1 = @{@"call" : @2};
    
    if ([db tableExists:@"BeeQuick"]) {
        
        [_AccountMgr POSTWithUrlString:shop_url parameters:para1 success:^(id responseObject) {
            
            FMDatabase *db1 = [[FMDatabase alloc] initWithPath:localDBPath];
            [db1 open];
            
            // 获取分类模型数组
            NSArray *dictArray = responseObject[@"data"];
            
            _shopArray = [NSArray yy_modelArrayWithClass:[BQRightModel class] json:dictArray];

            NSMutableArray *array = [NSMutableArray array];
            
            for (BQRightModel *model in _shopArray) {
                
                FMResultSet *set = [db1 executeQuery:@"select * from BeeQuick where id = ?",model.id];
                
                while ([set next]) {
                    BQRightModel *model = [[BQRightModel alloc] init];
                    
                    model.name = [set stringForColumn:@"name"];
                    model.market_price = [set stringForColumn:@"market_price"];
                    model.partner_price = [set stringForColumn:@"partner_price"];
                    model.pm_desc = [set stringForColumn:@"pm_desc"];
                    model.img = [set stringForColumn:@"img"];
                    model.specifics = [set stringForColumn:@"specifics"];
                    model.count = [set intForColumn:@"count"];
                    model.category_id = [set stringForColumn:@"category_id"];
                    model.id = [set stringForColumn:@"id"];
                    
                    [array addObject:model];
       
                }

            }
            
            _shopArray = array.copy;
            
            [db1 close];
            
            [self.homeCV reloadData];
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
        
        
    }else{
        
        [_AccountMgr POSTWithUrlString:shop_url parameters:para1 success:^(id responseObject) {
            
            NSArray *dictArray = responseObject[@"data"];
            
            _shopArray = [NSArray yy_modelArrayWithClass:[BQRightModel class] json:dictArray];
            
            [self.homeCV reloadData];
            
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
        
    }
    
    [db close];
    
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self.homeCV reloadData];
    
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
}



@end
